package com.promis.party.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.promis.compat.StatusBarCompat;
import com.promis.party.R;
import com.promis.party.helper.ContextHelper;
import com.promis.party.helper.LoginHelper;
import com.promis.party.preference.AccountPreferences;


public class WelcomeActivity extends AppCompatActivity {

	private ContextHelper contextHelper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		StatusBarCompat.translucentStatusBar(this);
		StatusBarCompat.blackTextStatusBar(this);
		setContentView(R.layout.activity_welcome);
		initData();
		httpInitData();
		dispatcher();
	}

	private void initData() {
		contextHelper = new ContextHelper(getBaseContext());
	}

	public void httpInitData() {
//		String url = contextHelper.getServiceUrl(R.string.initData);
//		HttpJsonParams params = new HttpJsonParams();
//		new HttpClient<JsonInitData>(getBaseContext()) {
//
//			@Override
//			protected void onSuccess(JsonInitData result) {
//				if (!CollectionUtils.isNullOrEmpty(result.getDogCategoryList())) {
//					CategoryPreferences.getInstance(getBaseContext()).putDogCategorys(result.getDogCategoryList());
//				}
//				if (!CollectionUtils.isNullOrEmpty(result.getCatCategoryList())) {
//					CategoryPreferences.getInstance(getBaseContext()).putCatCategorys(result.getCatCategoryList());
//				}
//				if (!CollectionUtils.isNullOrEmpty(result.getDogHotspotCategoryList())) {
//					CategoryPreferences.getInstance(getBaseContext()).putDogHotspotCategorys(result.getDogHotspotCategoryList());
//				}
//				if (!CollectionUtils.isNullOrEmpty(result.getCatHotspotCategoryList())) {
//					CategoryPreferences.getInstance(getBaseContext()).putCatHotspotCategorys(result.getCatHotspotCategoryList());
//				}
//
//				if (result.getAndroidVersion() != null) {
//					VersionPreferences.getInstance(getBaseContext()).put(result.getAndroidVersion());
//				}
//				if (result.getFastdfsConfig() != null) {
//					FastdfsConfigPreferences.getInstance(getBaseContext()).put(result.getFastdfsConfig());
//				}
//				if (FastdfsConfigPreferences.getInstance(getBaseContext()).exists()) {
//					FastFileStorageClient.init(FastdfsConfigPreferences.getInstance(getBaseContext()).get());
//				}
//				if (result.getRemindHelpList() != null){
//					RemindHelpPreferences.getInstance(getBaseContext()).put(JsonUtils.toJson(result.getRemindHelpList()));
//				}
//			}
//
//			@Override
//			protected void onFinish() {
//				dispatcher();
//			}
//
//			@Override
//			protected void onNetworkDisconnect() {
//				super.onNetworkDisconnect();
//				dispatcher();
//			}
//		}.postAsyn(url, params);
	}

	private void dispatcher() {
		if (AccountPreferences.getInstance(this).exists()) {
			httpLogin();
		} else {
			Intent intent = new Intent(getBaseContext(), LoginActivity.class);
			startActivity(intent);
			WelcomeActivity.this.finish();
		}
	}

	private void httpLogin() {
		try {
			LoginHelper.doLogin(contextHelper, new LoginHelper.UserLoginListener() {
				@Override
				public void onFinish() {
					Intent intent = new Intent(getBaseContext(), MainActivity.class);
					startActivity(intent);
					WelcomeActivity.this.finish();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
